package com.aceitunaproject.server.message.processor;


import java.util.logging.Logger;

import com.aceitunaproject.server.player.Player;
import com.aceitunaproject.shared.exceptions.gamerules.IllegalActionException;
import com.aceitunaproject.shared.message.Message;
import com.aceitunaproject.shared.message.concrete.BuildInstallationRequest;

import com.aceitunaproject.shared.message.concrete.RuleViolationEvent;

/**
 * Handles the "Build Installation" action request. Builds a installation of the given type with the unit in the given map
 * tile, if the action is legal
 * 
 * @author Federico Bergstein
 * 
 */
public class BuildInstallationRequestProcessor implements MessageProcessor {

	private static Logger logger = Logger.getLogger(BuildInstallationRequestProcessor.class
			.getName());

	/**
	 * {@inheritDoc}
	 */
	@Override
	public void processMessage(Player player, Message message) {

		BuildInstallationRequest buildRequest = (BuildInstallationRequest) message;

		// TODO: Implement single logging
		logger.info("ordered to build installation " + buildRequest.getName()  + " with unit " + buildRequest.getElementId()
				+ " in " + buildRequest.getPositionX() + " , "
				+ buildRequest.getPositionY());
		
		try {

			player.buildInstallation(buildRequest.getElementId(),buildRequest.getInstallationType(),buildRequest.getName(), buildRequest.getPositionX(),
					buildRequest.getPositionY());

		} catch (IllegalActionException e) {
			
			RuleViolationEvent ruleViolation = new RuleViolationEvent();
			ruleViolation.setIllegalActionMessageID(message.getId());
			ruleViolation.setRuleViolations(e);
			player.sendObjectMessage(ruleViolation);
		}

	}

}
